//
//  baseViewController.m
//  PostMessa
//
//  Created by never down on 15-5-25.
//  Copyright (c) 2015年 ygy_ios. All rights reserved.
//

#import "baseViewController.h"
#import "mainViewController.h"
#import "PostMessa-Prefix.pch"
@interface baseViewController() <UITableViewDataSource, UITableViewDelegate>
{
    NSInteger selected;  //选中的分类在 table 中的索引
    UITableView *classV; //分类列表

}

@end

@implementation baseViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.userInteractionEnabled = YES;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(slideUpdate) name:@"slideUpdate" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(classSelect:) name:@"classSelect" object:nil];
    
    [self slideInit];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#define SLIDEWIDTH 145  //侧边栏宽度
#define SLIDEMARGINTOP 20
#define SLIDELABELHEIGHT 20
#define SLIDECLASSMARGIN 15
#define SLIDECLASSMARGININNER 10
//分类侧边栏
- (void)slideInit {
    UIView *slideV = [[UIView alloc] initWithFrame: CGRectMake(0, 0, SLIDEWIDTH, self.view.bounds.size.height)];
    [slideV setBackgroundColor:ERHUORED(1.0f)];
    //slideV.tag = BUTTONTAG * 4 - 1;
    slideV.userInteractionEnabled = YES;
    
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(0, SLIDEMARGINTOP + STATUSHEIGHT, slideV.bounds.size.width, SLIDELABELHEIGHT)];
    [titleL setText:CLASS];
    [titleL setTextColor:[UIColor whiteColor]];
    [titleL setFont:[UIFont systemFontOfSize:20]];
    [titleL setTextAlignment:NSTextAlignmentCenter];
    [slideV addSubview:titleL];
    
    [self.view addSubview:slideV];
    
    classV = [[UITableView alloc] initWithFrame:CGRectMake(0,
                STATUSHEIGHT + SLIDEMARGINTOP + SLIDELABELHEIGHT + SLIDECLASSMARGIN,
                                                           slideV.bounds.size.width,
        slideV.bounds.size.height - (STATUSHEIGHT + SLIDEMARGINTOP + SLIDELABELHEIGHT + SLIDECLASSMARGIN)) style:UITableViewStylePlain];
    [classV setBackgroundColor:ERHUORED(0.0f)];
    //设置其无内容显示时 显示为空白，因此无下划线
    [classV setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    classV.bounces = NO;
    classV.rowHeight = SLIDECLASSMARGIN * 2 + SLIDELABELHEIGHT;
    classV.delegate = self;
    classV.dataSource = self;
    [slideV addSubview:classV];
    [classV reloadData];
    
    
    mainViewController *mainVC = [[mainViewController alloc] init];
    [self addChildViewController:mainVC];
    [self.view addSubview:mainVC.view];

}

- (void)slide {
    mainViewController *mainVC = [[mainViewController alloc] init];
    [self addChildViewController:mainVC];
    [self.view addSubview:mainVC.view];

//    [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
////        UIViewController *mainVC = self.tabBarController;
//        if (self.view.frame.origin.x == 0)
//        {
//            self.view.frame = CGRectMake(SLIDEWIDTH, 0, self.view.frame.size.width, self.view.frame.size.height);
//        }else {
//            self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
//        }
//    }completion:nil];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [(NSArray *)[[NSUserDefaults standardUserDefaults] objectForKey:@"classData"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"menuClassList";
    UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        UIView *contentV = [[UIView alloc]initWithFrame:CGRectMake(SLIDECLASSMARGIN, SLIDECLASSMARGIN, tableView.frame.size.width, SLIDELABELHEIGHT)];
        contentV.tag = BUTTONTAG;
        [cell addSubview:contentV];
        [cell setBackgroundColor:ERHUORED(0.0f)];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary *data = [[[NSUserDefaults standardUserDefaults] objectForKey:@"classData"] objectAtIndex:indexPath.row];
        
        UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SLIDELABELHEIGHT, SLIDELABELHEIGHT)];
        NSString *img = [data valueForKey:@"small_icon"];
        if ([img length] > 0) {
            [imgV initWithImage:[UIImage imageNamed:img]];
        }
        imgV.contentMode = UIViewContentModeScaleAspectFit;
        [contentV addSubview:imgV];
        
        UILabel *titleL = [[UILabel alloc] initWithFrame:
                           CGRectMake(SLIDELABELHEIGHT + SLIDECLASSMARGININNER,
                                      0,
                                      cell.frame.size.width - (SLIDELABELHEIGHT + SLIDECLASSMARGININNER), SLIDELABELHEIGHT)];
        [titleL setText:[data objectForKey:@"cname"]];
        [titleL setTextColor:[UIColor whiteColor]];
        [titleL setTextAlignment:NSTextAlignmentLeft];
        [titleL setFont:[UIFont systemFontOfSize:17]];
        [contentV addSubview:titleL];
        if (indexPath.row != selected) {
            contentV.alpha = 0.5f;
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *select = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selected inSection:0]];
    [select viewWithTag:BUTTONTAG].alpha = 0.5f;
    selected = indexPath.row;
    select = [tableView cellForRowAtIndexPath:indexPath];
    [select viewWithTag:BUTTONTAG].alpha = 1.0f;
    //改变标题
    NSDictionary *data = [[[NSUserDefaults standardUserDefaults] objectForKey:@"classData"] objectAtIndex:indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"classChanged" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[data objectForKey:@"cid"], @"cid", [data objectForKey:@"cname"], @"cname", nil]];
}

@end
